package com.g4m.baselib.entity;

import lombok.Data;

/**
 * Created on 2020-01-01
 * <p>Copyright 2008-2020 snsndk.com</p>
 *
 *
 */
@Data
public class RequestPager {

    /**
     * 倒序
     */
    public static final int ORDER_BY_DESC = 1;

    /**
     * 正序
     */
    public static final int ORDER_BY_ASC = 2;

    private int page = 1;

    private int limit = 20;

    private long cursor = 0;

    /**
     * 默认倒叙
     */
    private int sort = ORDER_BY_ASC;

    private int startNum;

    public int getPage() {
        return Math.max(1, page);
    }

    public int getLimit() {
        return Math.min(100, Math.max(1, limit));
    }

    public int getStartNum() {
        return (getPage() - 1) * getLimit();
    }

    public int getTotalPage(int total) {
        return (int) Math.ceil((double) total / (double) getLimit());
    }

    public long getCursor() {
        //第一页,切排序为倒序,则设置游标为最大值
        if (cursor == 0L && sort == ORDER_BY_DESC) {
            return Long.MAX_VALUE;
        }
        return cursor;
    }
}
